package com.shu.activemq.producer.impl;

import com.shu.activemq.producer.IProducerService;
import com.shu.controller.LoginController;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jms.core.JmsTemplate;
import org.springframework.jms.core.MessageCreator;
import org.springframework.stereotype.Service;

import javax.jms.Destination;
import javax.jms.JMSException;
import javax.jms.Message;
import javax.jms.Session;

@Service("producerService")
public class ProducerServiceImpl  implements IProducerService {
    @Autowired
    private JmsTemplate jmsTemplate;
    //日志
    private Logger logger = LoggerFactory.getLogger(ProducerServiceImpl.class);

    /**
     * 发送消息
     * @param destination
     * @param msg
     */
    public void sendMessage(Destination destination,final String msg) {
        logger.info("sendMessage  destination:{} msg:{}",destination,msg);
        MessageCreator messageCreator = new MessageCreator(){
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(msg);
            }
        };
        jmsTemplate.send(destination, messageCreator);
    }

    public void sendMessage(final String msg) {
        String destination = jmsTemplate.getDefaultDestinationName().toString();
        logger.info("sendMessage  destination:{} msg:{}",destination,msg);
        MessageCreator messageCreator = new MessageCreator(){
            public Message createMessage(Session session) throws JMSException {
                return session.createTextMessage(msg);
            }
        };
        jmsTemplate.send(messageCreator);
    }

}
